namespace ObjectPersistence {
    using System;
    using System.Text;
    using System.Collections;
    using System.Data.Common;
    using System.Transactions;
    using System.Collections.Generic;
    
    public partial class StaticHitCountAssembler : AssemblerBaseUpdatable<KYO.DAL.StaticHitCount, StaticHitCountCriteria, StaticHitCountRowMap, StaticHitCountDomainObjectFactory> {
        public StaticHitCountAssembler() {
            base.connection = new System.Data.SqlClient.SqlConnection(GlobalUtil.Constants.ConnectionString);
        }
        protected override System.Data.Common.DbConnection InstanceConnection {
            get {
                return this.connection;
            }
        }
        /// <summary>Reads a single object from the database corresponding to the object keys which are passed in</summary>
        public KYO.DAL.StaticHitCount ReadSingleByKey(int ContentId, int Month, byte Type, int Year) {
            StaticHitCountCriteria criteria = new StaticHitCountCriteria();
            criteria.Set_ContentId(StaticHitCountCriteria.AVAILABLE_OPERATORS.EQUALS, ContentId);
            criteria.Set_Year(StaticHitCountCriteria.AVAILABLE_OPERATORS.EQUALS, Year);
            criteria.Set_Month(StaticHitCountCriteria.AVAILABLE_OPERATORS.EQUALS, Month);
            criteria.Set_Type(StaticHitCountCriteria.AVAILABLE_OPERATORS.EQUALS, Type);
            return this.ReadSingle(criteria);
        }

        internal override void MakePersistent(KYO.DAL.StaticHitCount PersistentObject, bool cascade, System.Data.Common.DbCommand comm) {
            if ((PersistentObject.IsNew == false)) {
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) 
                            && (PersistentObject.ChangesMade == false))) {
                    return;
                }
                if ((GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Contains(PersistentObject.ROKeyString) == false)) {
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                }
            }

            if ((PersistentObject.IsNew || PersistentObject.ChangesMade)) {
                StaticHitCountUpdateFactory updateFactory = new StaticHitCountUpdateFactory();
                comm.Parameters.Clear();
                if ((PersistentObject.IsNew == false)) {
                    updateFactory.updateCommandText(PersistentObject, comm);
                    int numRows = comm.ExecuteNonQuery();
                    if ((numRows == 0)) {
                        throw new NoRowsUpdated();
                    }
                }
                else {
                    updateFactory.insertCommandText(PersistentObject, comm);
                    comm.ExecuteNonQuery();
                    GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Add(PersistentObject.ROKeyString);
                    PersistentObject.IsNew = false;
                }
            }
            PersistentObject.ChangesMade = false;

        }

        internal override void Delete(KYO.DAL.StaticHitCount PersistentObject, System.Data.Common.DbCommand comm) {
            comm.Parameters.Clear();

            StaticHitCountSelectionFactory tempStaticHitCountSelectionFactory = new StaticHitCountSelectionFactory();
            string sql = "DELETE FROM [dbo].[StaticHitCount]";
            comm.CommandText = sql;
            tempStaticHitCountSelectionFactory.getDefaultSelection(PersistentObject, comm);
            comm.ExecuteNonQuery();
        }

        #region Helper Utilities
        protected override StaticHitCountRowMap fillRowMap(System.Data.Common.DbDataReader reader) {
            StaticHitCountRowMap result = new StaticHitCountRowMap();
            result.ContentId = reader.GetInt32(reader.GetOrdinal("dboStaticHitCountDOTContentId"));
            result.Year = reader.GetInt32(reader.GetOrdinal("dboStaticHitCountDOTYear"));
            result.Month = reader.GetInt32(reader.GetOrdinal("dboStaticHitCountDOTMonth"));
            result.Type = reader.GetByte(reader.GetOrdinal("dboStaticHitCountDOTType"));
            result.HitCount = reader.GetInt32(reader.GetOrdinal("dboStaticHitCountDOTHitCount"));
            return result;
        }
        internal override void ClearList() {
            GlobalUtil.Constants.ObjectMapKYOPHUONGDOPC.Clear();
        }

        public override string Abstract_SELECT {
            get {
                return ("SELECT " + StaticHitCountAssembler.RawFields);
            }
        }
        protected override string InstanceTable {
            get {
                return "[dbo].[StaticHitCount]";
            }
        }
        public const string RawFields = @"[dbo].[StaticHitCount].[ContentId] AS dboStaticHitCountDOTContentId, [dbo].[StaticHitCount].[Month] AS dboStaticHitCountDOTMonth, [dbo].[StaticHitCount].[Type] AS dboStaticHitCountDOTType, [dbo].[StaticHitCount].[Year] AS dboStaticHitCountDOTYear, [dbo].[StaticHitCount].[HitCount] AS dboStaticHitCountDOTHitCount";
        public const string SELECT = @"SELECT [dbo].[StaticHitCount].[ContentId] AS dboStaticHitCountDOTContentId, [dbo].[StaticHitCount].[Month] AS dboStaticHitCountDOTMonth, [dbo].[StaticHitCount].[Type] AS dboStaticHitCountDOTType, [dbo].[StaticHitCount].[Year] AS dboStaticHitCountDOTYear, [dbo].[StaticHitCount].[HitCount] AS dboStaticHitCountDOTHitCount";
        #endregion
    }
}
